package com.yin.myproject;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author Eason
 * @create 2018-04-01 13:59
 **/
public class ClassApiForGenericSuperclass {

    public static void main(String[] args) {
        Class<UserService> clazz = UserService.class;
        Class genericClazz;
        Type type = clazz.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            //返回表示此类型实际类型参数的 Type 对象的数组
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            genericClazz = (Class) actualTypeArguments[0];
        } else {
            genericClazz = (Class) type;
        }
        if(genericClazz != null){
            System.out.println(genericClazz.getName());
        }
    }

    class User {

        private Integer id;
        private String name;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    class BaseHibernateDao<T, ID extends Serializable> {

    }

    class UserService extends BaseHibernateDao<User, Integer> {

    }
}
